package com.test.project.system.dajuan.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.OptionalDouble;
import java.util.stream.DoubleStream;

import org.apache.commons.lang.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.test.common.utils.poi.ExcelUtil;
import com.test.framework.web.controller.BaseController;
import com.test.framework.web.domain.AjaxResult;
import com.test.framework.web.page.TableDataInfo;
import com.test.project.system.dajuan.domain.TDajuan;
import com.test.project.system.dajuan.service.ITDajuanService;
import com.test.project.system.dati.domain.TDati;
import com.test.project.system.dati.service.ITDatiService;
import com.test.project.system.pingfen.domain.TPingfen;
import com.test.project.system.pingfen.service.ITPingfenService;
import com.test.project.system.wenjuan.service.ITWenjuanService;

/**
 * 用户问卷Controller
 * 
 * @author test
 * @date 2023-12-05
 */
@Controller
@RequestMapping("/system/dajuan")
public class TDajuanController extends BaseController
{
    private String prefix = "system/dajuan";
    private String wprefix = "system/wenjuan";

    @Autowired
    private ITDajuanService tDajuanService;

    @Autowired
    private ITWenjuanService tWenjuanService;

    @Autowired
    private ITDatiService tDatiService;
    

    @Autowired
    private ITPingfenService pingfenService;

    @GetMapping()
    public String dajuan()
    {
        return prefix + "/dajuan";
    }
    
    @GetMapping("/cansai")
    public String cansai()
    {
        return prefix + "/cansai";
    }

    /**
     * 查询用户问卷列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TDajuan tDajuan)
    {
//        startPage();
        if(getSysUser().getRoleId() == 2L) {
        	tDajuan.setCreateBy(getLoginName());
        }
        List<TDajuan> list = tDajuanService.selectTDajuanList(tDajuan);
        list.forEach(e->{
        	if(e.getStatus().equals("1")) {
        		TPingfen pingfen = new TPingfen(); 
    			pingfen.setPid(e.getId());
            	List<TPingfen> pingfenList = pingfenService.selectTPingfenList(pingfen);
            	
            	if(pingfenList.size()<3) {
            		return;
            	}
            	
            	  OptionalDouble average = pingfenList.stream().mapToDouble(TPingfen::getScore).sorted().skip(1).limit(pingfenList.size()-2).average();
            	 
            	 e.setScore(average.getAsDouble());
        	}
        });
        
        list.sort(Comparator.comparing(TDajuan::getScore).reversed());
        for (int i = 0; i < list.size(); i++) {
        	list.get(i).setRank(i+1);
		}
        return getDataTable(list);
    }

    /**
     * 导出用户问卷列表
     */
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TDajuan tDajuan)
    {
        List<TDajuan> list = tDajuanService.selectTDajuanList(tDajuan);
        ExcelUtil<TDajuan> util = new ExcelUtil<TDajuan>(TDajuan.class);
        return util.exportExcel(list, "dajuan");
    }

    /**
     * 新增用户问卷
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存用户问卷
     */
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TDajuan tDajuan)
    {
        return toAjax(tDajuanService.insertTDajuan(tDajuan));
    }

    /**
     * 修改用户问卷
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        TDajuan tDajuan = tDajuanService.selectTDajuanById(id);
        mmap.put("tDajuan", tDajuan);
        return prefix + "/edit";
    }

    /**
     * 修改保存用户问卷
     */
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TDajuan tDajuan)
    {
        return toAjax(tDajuanService.updateTDajuan(tDajuan));
    }

    /**
     * 删除用户问卷
     */
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(tDajuanService.deleteTDajuanByIds(ids));
    }
    
    @GetMapping("/show/{postId}")
    public String show(@PathVariable("postId") Long postId, ModelMap mmap)
    {
    	TDajuan dajuan = tDajuanService.selectTDajuanById(postId);
    	mmap.put("post", dajuan.getWenjuan());
  
    		TDati tDati = new TDati();
    		tDati.setDajuanId(dajuan.getId());
    		List<TDati> datis = tDatiService.selectTDatiList(tDati);
    		mmap.put("datis", datis);
    		return wprefix + "/show1";
    
    	
    }
}
